package com.wash.shoes.controller.pc;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.utils.StringUtils;
import com.wash.shoes.domain.dy.request.DyRefundRequest;
import com.wash.shoes.domain.vo.HaoHanUploadBarCodeVO;
import com.wash.shoes.domain.washer.WashReturn;
import com.wash.shoes.domain.washer.WasherDto;
import com.wash.shoes.entity.OrderPo;
import com.wash.shoes.exception.WashException;
import com.wash.shoes.service.OrderService;
import com.wash.shoes.service.HttpClientService;
import com.wash.shoes.task.RyWashTask;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;


/**
 * @author lijianguo
 * @Date 21/09/2024 首页的数据
 * 请输入类的简介
 **/
@Api(tags = {"PC-首页"})
@RestController
@RequestMapping({"/firstPage", "/dev-api/firstPage"})
public class FirstPageController {

    @Autowired
    OrderService orderService;

    @Autowired
    HttpClientService httpClientService;

    @Autowired
    DyRefundRequest  dyRefundRequest;

    @Autowired
    RyWashTask ryWashTask;

    @ApiOperation("获首页统计的数据")
    @GetMapping("/firstCount")
    public R<Map> firstCount(String beginDate) {
        Map dataCount = orderService.firstCount(beginDate);
        return R.ok(dataCount);
    }

    @ApiOperation("测试上传条码")
    @GetMapping("/audoSorting")
    public R<WashReturn> audoSorting() {

        WashReturn washReturn = httpClientService.sendPostTiaoMa(buildWashDto());
        return R.ok(washReturn);
    }

    @ApiOperation("测试查询条码")
    @GetMapping("/queryUploadTiaoMa")
    public R<List<HaoHanUploadBarCodeVO>> queryUploadTiaoMa() {

        List<HaoHanUploadBarCodeVO> stringResponseEntity = httpClientService.queryTiaoMa("12333");

        System.out.println("Single Response: " + stringResponseEntity);
        return R.ok(stringResponseEntity);
    }


    public static List<WasherDto> buildWashDto() {
        List<WasherDto> list = new ArrayList<>();
        WasherDto washerDto = new WasherDto();
        washerDto.setTiaoMa("333");
        washerDto.setOrdersNumber("20250210111222333");
        washerDto.setPeiJianNum(Integer.valueOf(33));
        washerDto.setYiWuNum(Integer.valueOf(0));
        washerDto.setIsPeiJian(false);
        washerDto.setRuChangTime("2025-02-10");
        list.add(washerDto);
        return list;
    }


    @ApiOperation("查询退款")
    @GetMapping("/queryRefund")
    public R<String> queryRefund(String orderNo) {
        OrderPo orderPo = orderService.getOne(new LambdaQueryWrapper<OrderPo>().eq(StringUtils.isNotBlank(orderNo),OrderPo::getOrderNo ,orderNo));
        if(Objects.isNull(orderPo) || !StringUtils.equals(orderPo.getComeFrom(),"3")) {
            throw new WashException("订单为空或者订单号来源渠道不正确!");
        }
        if(StringUtils.isNotBlank(orderPo.getDyBookCode())) {
            String queryRefund = dyRefundRequest.queryRefund(orderPo);
            return R.ok(queryRefund);
        }
        return R.ok("订单未预约无法校验退款");
    }
}
